<template>
  <div class="personal-main">
    <el-table :data="borrowInfoList" style="width: 100%">
      <el-table-column prop="amount" label="借款金额"> </el-table-column>
      <el-table-column prop="period" label="期数"> </el-table-column>
      <el-table-column prop="borrowYearRate" label="年化率"> </el-table-column>
      <el-table-column prop="params.returnMethod" label="还款方式">
      </el-table-column>
      <el-table-column prop="params.moneyUse" label="资金用途">
      </el-table-column>
      <el-table-column prop="createTime" label="申请时间"> </el-table-column>
      <el-table-column prop="params.status" label="状态">
        <template slot-scope="scope">
          <el-tag type="success">{{ scope.row.params.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 1"
            @click="onCancel(scope.$index, scope.row)"
            type="success"
            >撤销申请</el-button
          >
          <el-button v-else-if="scope.row.status == -2" disabled type="success"
            >已撤销</el-button
          >
          <el-button v-else disabled type="success">撤销申请</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[1, 5, 10, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data() {
    return {
      borrowInfoList: [],
      pageNum: 1,
      pageSize: 3,
      total: 0,
    };
  },
  methods: {
    onCancel(index, row) {
      this.$confirm("您确认要取消该次借款申请吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let token = localStorage.getItem("SRB-TOKEN");
          if (!token) {
            this.$message.error("登陆已过期");
            return;
          }
          this.$axios({
            method: "PUT",
            url: `/api/core/borrowInfo/cancel/${row.id}`,
            headers: { token },
          }).then((r) => {
            this.$message({
                type:'success',
                message: '撤销成功'
            });
            this.initData();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "什么也没做!",
          });
        });
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.initData();
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.initData();
    },
    initData() {
      let token = localStorage.getItem("SRB-TOKEN");
      if (!token) {
        this.$message("登录信息已过期");
        return;
      }
      this.$axios({
        method: "GET",
        url: `/api/core/borrowInfo/${this.pageNum}/${this.pageSize}`,
        headers: { token },
      }).then((r) => {
        this.borrowInfoList = r.data.data.page.records;
        this.total = Number(r.data.data.page.total);
      });
    },
  },
  mounted() {
    this.initData();
  },
};
</script>